//
//  WQComposeViewController.swift
//  WeiBo
//
//  Created by 张德华 on 7/26/16.
//  Copyright © 2016 ZhangDeHua. All rights reserved.
//

import UIKit

class WQComposeViewController: UIViewController,UITextViewDelegate {
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        setupUI()
        
        setupNav()
        
    }
    
    
    func setupUI() {
        
        view.addSubview(composeTextView)
        composeTextView.addSubview(pictureView)
        view.addSubview(toolBarView)
        
        composeTextView.snp_makeConstraints { (make) in
            make.top.bottom.equalTo(view)
            make.width.equalTo(view)
        }
        pictureView.snp_makeConstraints { (make) in
            make.centerX.equalTo(view)
            make.top.equalTo(100)
            make.size.equalTo(CGSize(width: SCREENSIZE.width-20, height: SCREENSIZE.width-20))
        }
        toolBarView.snp_makeConstraints { (make) in
            make.leading.bottom.trailing.equalTo(view)
            make.height.equalTo(44)
        }
        
        
        
        toolBarView.closure = {(type: WQToolBarViewType) -> () in
            
            switch type {
            case .Picture:
                print("101")
            case .Metion:
                print("102")
            case .Trend:
                print("103")
            case .Emoticon:
                print("104")
            case .Add:
                print("105")
                
            }
        }
        
        // TODO: - 监听配图中加号cell点击,弹出相册
        pictureView.closure = {[weak self] in
            self?.selectImage()
            
        }
    }
    
    
    func setupNav() {
        
        navigationItem.leftBarButtonItem = UIBarButtonItem(title: "取消", style: UIBarButtonItemStyle.Plain, target: self, action: #selector(WQComposeViewController.cancelButtonClicked))
        
        navigationItem.rightBarButtonItem = UIBarButtonItem(title: "发布", style: UIBarButtonItemStyle.Plain, target: self, action: #selector(WQComposeViewController.sendButtonClicked))
        
        navigationItem.rightBarButtonItem?.enabled = false
        navigationItem.titleView = titleViewLabel
        
    }
    
    
    // MARK: - ************导航按钮监听事件
    ///------ 取消
    func cancelButtonClicked() {
        dismissViewControllerAnimated(true, completion: nil)
    }
    
    ///------ 发布
    func sendButtonClicked() {
        print("发布")
    }
    
    
    
    // MARK: - ************标题
    private lazy var titleViewLabel: UILabel = {
        
        let name = WQUserAccountModel.sharedUserAccount.screen_name!
        let str = "发微博\n\(name)"
        let range = (str as NSString).rangeOfString(name)
        let attr = NSMutableAttributedString(string: str)
        attr.addAttributes([NSForegroundColorAttributeName: ThemeColor, NSFontAttributeName: UIFont.systemFontOfSize(12)], range: range)
        
        let label = UILabel()
        label.attributedText = attr
        label.numberOfLines = 0
        label.textAlignment = .Center
        label.sizeToFit()
        return label
    }()
    
    
    
    
    
    
    
    
    
    // MARK: - ************懒加载
    private lazy var composeTextView: WQComposeTextView = {
        
        let view = WQComposeTextView()
        view.font = UIFont.systemFontOfSize(14)
        view.placeholder = "由赵德胤执导，柯震东、吴可熙主演的[再见瓦城]由赵德胤执导，柯震东、吴可熙主演的[再见瓦城]由赵德胤执导，柯震东、吴可熙主演的[再见瓦城]"
        view.backgroundColor = UIColor.whiteColor()
        view.delegate = self
        view.showsHorizontalScrollIndicator = false
        view.showsVerticalScrollIndicator = false
        view.alwaysBounceVertical = true
        return view
        
    }()
    
    
    private lazy var pictureView: WQComposePictureView = WQComposePictureView()
    
    private lazy var toolBarView: WQToolBarView = WQToolBarView()
    
    
    
    
    
}


extension WQComposeViewController {
    
    // MARK: - ************代理方法
    func textViewDidChange(textView: UITextView) {
        
        navigationItem.rightBarButtonItem?.enabled = composeTextView.hasText()
    }
}



// MARK: - ************底部图片按钮点击回调
extension WQComposeViewController {
    
    func selectImage() {
        
        let pickerVc = UIImagePickerController()
        
        ///------ 设置代理
        // TODO: - 代理
//        pickerVc.delegate = self
        self.presentViewController(pickerVc, animated: true, completion: nil)
        
        
        
    }
    
    
}


